package com.ghyf.mplay.bean;

import java.util.ArrayList;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

import com.ghyf.mplay.datatype.AlignMode;
import com.ghyf.mplay.datatype.BackParam;
import com.ghyf.mplay.datatype.FontParam;
import com.ghyf.mplay.datatype.GradientColorType;
import com.ghyf.mplay.datatype.POS;
import com.ghyf.mplay.datatype.RGBA;
import com.ghyf.mplay.datatype.ShowType;
import com.ghyf.mplay.util.TypeFaceFactory;
import com.ghyf.mplay.view.MText;

/**
 * 显示文本模块的信息包装类
 * @author 陈小红
 *
 */
public class TextInfo extends BaseModuleInfo {

	@Override
	public MText getView(Context context) {
		mContext=context;
		mText = new MText(context);
		//设置控件基本属性
		mText.setModule_type(module_type);
		mText.setzOrder(zOrder);
		mText.setFile_version(file_version);
		mText.setModule_name(module_name);
		mText.setModule_uid(module_uid);
		mText.setModule_gid(module_gid);
		mText.setmText(mText);
		mText.setShow_interval(show_interval);
		mText.setText_list(text_list);
		if(text_list!=null&&text_list.size()>=1){
			mText.setText(text_list.get(0));
		}
		
		// 显示方式
		// mText.setTextScaleX(0.5f);

		// 位置
		initPosition(mText, pos);

		// 背景
		initBackground(mText, backgroud);

		// 设置字体
		initFont(mText, font);

		//  对齐方式
		setGravity();

		// 设置左、上的padding（用left、top margin变量设置）
		mText.setPadding(left_margin, top_margin, 0, 0);

		// 设置Text
		return mText;

	}
	void setGravity(){
		switch (align) {
		case LOWLEFT:
			mText.setGravity(Gravity.LEFT|Gravity.BOTTOM);
			break;
		case LOWMIDDLE:
			mText.setGravity(Gravity.CENTER|Gravity.BOTTOM);
			break;
		case LOWRIGHT:
			mText.setGravity(Gravity.RIGHT|Gravity.BOTTOM);
			break;
		case MIDDLELEFT:
			mText.setGravity(Gravity.LEFT|Gravity.CENTER);
			break;
		case CENTER:
			mText.setGravity(Gravity.CENTER);
			break;
		case MIDDLERIGHT:
			mText.setGravity(Gravity.RIGHT|Gravity.CENTER);
			break;
		case UPLEFT:
			mText.setGravity(Gravity.LEFT|Gravity.TOP);
			break;
		case UPMIDDLE:
			mText.setGravity(Gravity.CENTER|Gravity.TOP);
			break;
		case UPRIGHT:
			mText.setGravity(Gravity.RIGHT|Gravity.TOP);
			break;
		default:
			break;
		}
	}
	// <Show_Interval>10</Show_Interval>
	// <Text_Show_Param>0</Text_Show_Param>
	// <Font Name="宋体" Size="40" Width="0" Escapement="0" Orientation="0"
	// Kerning="0" Spacing="0" Bold="0" Italic="0" Underline="0"
	// FaceColor="(0,0,0,255)" EdgeWidth="0" EdgeColor="(0,0,0,255)"
	// ShadowWidth="0" ShadowColor="(0,0,0,255)" ShadowAngle="0" />
	// <Text_List>
	// <Item Content="欢迎乘坐北京地铁" />
	// <Item Content="Wellcom to Beijing！" />
	// <Item Content="欢迎乘坐北京地铁10号线" />
	// <Item Content="欢迎各位领导莅临指导！" />
	// </Text_List>
	/**
	 * 设置当前View显示/隐藏
	 * 2014-5-21 下午3:25:01
	 * @param isShow
	 *
	 */
	public void setVisibility(boolean isShow){
		if(isShow){
			mText.setVisibility(View.VISIBLE);
		}else{
			mText.setVisibility(View.GONE);
		}
	}
	/**
	 * 设置TextView的字体
	 * 
	 * @param tv
	 *            textview控件
	 * @param font
	 *            字体
	 */
	void initFont(TextView tv, FontParam font) {
		// 大小
		tv.setTextSize(font.getSize());
		// 字体
		Typeface typeface = TypeFaceFactory.createTypeface(font.getName());
		tv.setTypeface(typeface);
		// 颜色
		RGBA rgba = font.getFaceColor();
		int color = Color.argb(rgba.getAlpha(), rgba.getRed(), rgba.getGreen(),
				rgba.getBlue());
		tv.setTextColor(color);

		
	}

	/**
	 * 初始化控件的背景
	 * 
	 * @param viwe
	 *            被设置的控件
	 * @param bg
	 *            控件的背景
	 */
	void initBackground(View view, BackParam bg) {
		switch (bg.getType()) {
		case NOTSHOW: // 背景不显示
			break;
		case GRADIENTCOLOR: // 背景类型为渐变色
			if (bg.getColorType() == GradientColorType.HORIZONTALGRADIENT) {
				//水平渐变 参考：url:http://blog.csdn.net/a_large_swan/article/details/7107126
				int startColor = Color.argb(bg.getGradientcolor1().getAlpha(), bg.getGradientcolor1().getRed(), bg.getGradientcolor1().getGreen(),
						bg.getGradientcolor1().getBlue());
				int endColor = Color.argb(bg.getGradientcolor2().getAlpha(), bg.getGradientcolor2().getRed(), bg.getGradientcolor2().getGreen(),
						bg.getGradientcolor2().getBlue());
				GradientDrawable gradientDrawable=new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,new int[]{startColor,endColor});
				view.setBackgroundDrawable(gradientDrawable);
			} else {
				// 垂直渐变
				int startColor = Color.argb(bg.getGradientcolor1().getAlpha(), bg.getGradientcolor1().getRed(), bg.getGradientcolor1().getGreen(),
						bg.getGradientcolor1().getBlue());
				int endColor = Color.argb(bg.getGradientcolor2().getAlpha(), bg.getGradientcolor2().getRed(), bg.getGradientcolor2().getGreen(),
						bg.getGradientcolor2().getBlue());
				GradientDrawable gradientDrawable=new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,new int[]{startColor,endColor});
				view.setBackgroundDrawable(gradientDrawable);
			}
			break;
		case PICTURE: // 背景类型为图片
			// 本地图片文件路径生成drawable
			Bitmap bitmap = BitmapFactory.decodeFile(bg.getBkfile().getPath());
			Drawable drawable = new BitmapDrawable(mContext.getResources(),bitmap);
			// 设置图片背景
			view.setBackgroundDrawable(drawable);
			break;
		case PURECOLOR: // 背景类型为纯色
			RGBA purecolor = bg.getPurecolor();
			view.setBackgroundColor(Color.argb(purecolor.getAlpha(),
					purecolor.getRed(), purecolor.getGreen(),
					purecolor.getBlue()));
			break;
		default: // 其他
			break;
		}
	}

	/**
	 * 初始化控件的位置
	 * 
	 * @param view
	 *            被设置的控件
	 * @param pos
	 *            控件的位置
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	void initPosition(View view, POS pos) {
		view.setX(pos.getLeft());
		view.setY(pos.getTop());
		view.setLayoutParams(new LayoutParams(pos.getWidth(), pos.getHeight()));
	}

	private Context mContext=null;
	// <Backgroud Type="3" PureColor="(0,0,0,255)" GradientColor1="(0,0,0,255)"
	// GradientColor2="(0,0,0,255)" BKFile="" GradientColorType="0"/>
	private BackParam backgroud;

	// <Align>5</Align>
	private AlignMode align;// 对齐方式

	// <Left_Margin>0</Left_Margin>
	private int left_margin;// 左边距

	// <Top_Margin>0</Top_Margin>
	private int top_margin;// 上边距

	// <Show_Interval>10</Show_Interval>
	private int show_interval;

	// <Show_Type>0</Show_Type>
	private ShowType show_type; // 文本显示方式。滚动|压缩|静止

	// <Font Name="宋体" Size="40" Width="0" Escapement="0" Orientation="0"
	// Kerning="0" Spacing="0" Bold="0" Italic="0" Underline="0"
	// FaceColor="(0,0,0,255)" EdgeWidth="0" EdgeColor="(0,0,0,255)"
	// ShadowWidth="0" ShadowColor="(0,0,0,255)" ShadowAngle="0" />
	private FontParam font; // 字体

	// <Text_List>
	// <Item Content="欢迎乘坐北京地铁" />
	// <Item Content="Wellcom to Beijing！" />
	// <Item Content="欢迎乘坐北京地铁10号线" />
	// <Item Content="欢迎各位领导莅临指导！" />
	// </Text_List>
	private ArrayList<String> text_list;// 待显示的文本列表
	private String showText;

	private MText mText;

	
	
	/**
	 * @return the showText
	 */
	public String getShowText() {
		return showText;
	}

	/**
	 * @param showText the showText to set
	 */
	public void setShowText(String showText) {
		this.showText = showText;
	}

	/**
	 * @return the mText
	 */
	public MText getmText() {
		return mText;
	}

	/**
	 * @param mText the mText to set
	 */
	public void setmText(MText mText) {
		this.mText = mText;
	}

	public BackParam getBackgroud() {
		return backgroud;
	}

	public void setBackgroud(BackParam backgroud) {
		this.backgroud = backgroud;
	}

	public AlignMode getAlign() {
		return align;
	}

	public void setAlign(AlignMode align) {
		this.align = align;
	}

	public int getLeft_margin() {
		return left_margin;
	}

	public void setLeft_margin(int left_margin) {
		this.left_margin = left_margin;
	}

	public int getTop_margin() {
		return top_margin;
	}

	public void setTop_margin(int top_margin) {
		this.top_margin = top_margin;
	}

	public int getShow_interval() {
		return show_interval;
	}

	public void setShow_interval(int show_interval) {
		this.show_interval = show_interval;
	}

	public ShowType getShow_type() {
		return show_type;
	}

	public void setShow_type(ShowType show_type) {
		this.show_type = show_type;
	}

	public FontParam getFont() {
		return font;
	}

	public void setFont(FontParam font) {
		this.font = font;
	}

	public ArrayList<String> getText_list() {
		return text_list;
	}

	public void setText_list(ArrayList<String> text_list) {
		this.text_list = text_list;
	}


}
